<?php
/**
 * Created by PhpStorm.
 * User: zty
 * Date: 2018/10/23
 * Time: 21:11
 */

namespace app\admin\controller;
use think\App;
use think\Controller;
use think\Request;

class Institution extends controller
{
    private $user_id;
    public function __construct(App $app = null)
    {
        parent::__construct($app);
        $this->user_id = session('admin');

    }

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index($page =1 ,$pageSize = 10, $search =[])
    {
        $data = model('Common/Institution')->getList($page,$pageSize,$search);
        return ['code'=>0,'msg'=>'数据获取成功','data'=>$data];
    }

    /**
     * 添加机构
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save($info=[])
    {
        $result = $this->validate(
            $info,
            'app\common\validate\Institution'
        );

        if (true !== $result) {
           $this->ajaxData(1,$result);
            return ['code'=>1,'msg'=>$result];
        }
        $info['founder'] = $this->user_id;

        $r = model('common/institution')->add_one($info);
        if($r){
            return ['code'=>0,'msg'=>'添加成功','data'=>$r];
        }else{
            return ['code'=>2,'msg'=>'添加失败'];
        }





    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        $info = model('Common/Institution')->get_one($id,['id','name','note']);
        return ['code'=>0,'msg'=>'数据获取成功','data'=>$info];
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update($id,$info=[])
    {

        if(!$id){
            return ['code'=>1,'msg'=>'参数错误'];
        }
        $result = $this->validate(
            $info,
            'app\common\validate\Institution'
        );
        if (true !== $result) {
            return ['code'=>2,'msg'=>$result];
        }
        $r = model('Common/Institution')->edit($id,$info);
        if($r){
            return ['code'=>0,'msg'=>'修改成功'];
        }else{
            return ['code'=>3,'msg'=>'修改失败'];

        }






    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        if(!$id){
            return ['code'=>1,'msg'=>'参数错误'];
        }
        $r = model('Common/Institution')->del($id);
        if($r){
            return ['code'=>0,'msg'=>'删除成功'];
        }else{
            return ['code'=>2,'msg'=>'删除失败'];
        }
    }

    //下拉菜单
    public function get_arr(){
        $data = model('common/Institution')->get_arr();
        return ['code'=>0,'msg'=>'数据获取成功','data'=>$data];
    }
}